import type { CityStoreState } from '@/types/store';

class CityStoreStorage {
  private readonly key = '$$cities';

  private readonly defaultStoreState: CityStoreState = {
    cityId: 'ALL',
    cityName: '全部',
    field: 'all',
  };

  get(): CityStoreState {
    const jsonStr = localStorage.getItem(this.key);
    if (!jsonStr) {
      return this.defaultStoreState;
    }

    return JSON.parse(jsonStr) as CityStoreState;
  }

  set(storeState: CityStoreState): void {
    localStorage.setItem(this.key, JSON.stringify(storeState));
  }

  init(): CityStoreState {
    localStorage.setItem(this.key, JSON.stringify(this.defaultStoreState));
    return this.defaultStoreState;
  }
}

export default new CityStoreStorage();